package com.ljk.utils;

import com.ljk.domain.entity.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;


public class SecurityUtils
{
    /**
     * 获取用户
     **/
    public static LoginUser getLoginUser()
    {
        LoginUser loginUser=new LoginUser();
        Authentication authentication = getAuthentication();
        Object principal = authentication.getPrincipal();
        if(principal.equals("anonymousUser")){
            return loginUser;
        }
        loginUser=(LoginUser) principal;
        return loginUser;
    }

    /**
     * 获取Authentication
     */
    public static Authentication getAuthentication() {
        return SecurityContextHolder.getContext().getAuthentication();
    }

    public static Boolean isAdmin(){
        Long id = getLoginUser().getUser().getId();
        return id != null && id.equals(1L);
    }

    public static Long getUserId() {
        LoginUser loginUser = getLoginUser();
        if(loginUser.getUser()==null){
            return null;
        }
        return loginUser.getUser().getId();
    }
}
